// pages/appointment/appointment.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    departmentList: { id: '', name: '请选择' },
    doctorList: { id: '', name: '请选择' },
    date: "2020-08-14",
    hospital: '',
    department: '',
    doctor: '',
    dtime: '',
    mobile: '',
    description: ''

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (wx.getStorageSync('userid')) {
      wx.request({
        url: api.HospitalList,
        data: { site: wx.getStorageSync('role') },
        method: 'GET',
        success: function (res) {
          that.setData({
            hospitalList: res.data.data//专家姓名
          })
        },
      })
    } else {
      wx.navigateTo({
        url: '/pages/signin/signin',
      })
    }
  },
  bindPickerHos: function (e) {
    var that = this;
    console.log('医院',e.detail.id)
    wx.request({
      url: api.DepartmentList,
      data: { id: e.detail.id,site: wx.getStorageSync('role') },
      method: 'GET',
      success: function (res) {
        that.setData({
          departmentList: res.data.data,//专家姓名
          hospital:e.detail.id
        })
      },
    })
  },
  bindPickerDep: function (e) {
    var that = this;
    console.log('科室',e.detail.id)
    wx.request({
      url: api.ClinicDoctorList,
      data: { id: e.detail.id, site: wx.getStorageSync('role') },
      method: 'GET',
      success: function (res) {
        console.log(res);
        that.setData({
          doctorList: res.data.data,//专家姓名
          department: e.detail.id
        })
      },
    })
  },
  bindPickerDoc: function (e) {
    console.log('医生',e.detail.id)
    this.setData({
      doctor: e.detail.id
    })
  },
  bindDateChange: function(e){
    console.log('时间',e.detail.value);
    this.setData({
      date: e.detail.value
    })
  },
  inpphone: function (e) {
    //获取健康档案查询条件
    console.log('电话',e.detail.value);
    this.setData({
      mobile: e.detail.value
    })
  },
  inpdescription: function (e) {
    //获取健康档案查询条件
    console.log('描述',e.detail.value);
    this.setData({
      description: e.detail.value
    })
  },
  bindButtonTap: function (){
    var that = this;
    wx.request({
      url: api.AddAppointment,
      data: { 
        userId: wx.getStorageSync('userid'),
        site: wx.getStorageSync('role'),
        hospital: that.data.hospital, 
        department: that.data.department,
        doctor: that.data.doctor,
        dtime: that.data.date,
        mobile: that.data.mobile,
        description: that.data.description
      },
      method: 'Post',
      success: function (res) {
        if(res.data.code==200){
          wx.showToast({
            title: '预约成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/subscribe/subscribe',
            })
          }, 2500)
        }else if(res.data.code==300) {
          wx.showToast({
            title: '预约信息不完整',
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '预约失败',
            icon: 'none',
            duration: 2000
          })
        }
        
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})